<?php

declare(strict_types=1);

namespace App\Controller;


use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;


/**
 * Class UploadController
 * @package App\Controller
 * @AutoController()
 */
class UploadController extends AbstractController
{


    public function photo(RequestInterface $request, ResponseInterface $response)
    {
        if (!$request->hasFile('photo')) {
            return $response->json(['status' => STATUS_FAILURE, 'message' => '文件不存在']);
        }

        $uploadDir = sprintf('%s/storage/uploads', getcwd());
        $file = $request->file('photo');
        $filename = sprintf('%s.%s', time(), $file->getExtension());
        $file->moveTo(sprintf('%s/%s', $uploadDir, $filename));
        return $response->json(['status' => STATUS_SUCCESS, 'data' => [
            'file_url' => sprintf('http://106.13.168.150:9501/uploads/%s', $filename)
        ]]);
    }
}
